package com.example.cm_player.manager

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.cm_player.model.SongInfo
import com.example.cm_player.model.PlayMode

object PlayerStateManager {
    // 当前播放的音乐
    private val _currentPlayedMusic = MutableLiveData<SongInfo?>()
    val currentPlayedMusic: LiveData<SongInfo?> = _currentPlayedMusic

    // 是否正在播放
    private val _isPlaying = MutableLiveData(false)
    val isPlaying: LiveData<Boolean> = _isPlaying

    // 当前播放的进度
    private val _currentProgress = MutableLiveData(0L)
    val currentProgress: LiveData<Long> = _currentProgress

    // 歌曲的总共时长
    private val _duration = MutableLiveData(0L)
    val duration: LiveData<Long> = _duration

    // 当前播放模式
    private val _currentPlayMode = MutableLiveData<PlayMode>(PlayMode.LIST_LOOP)
    val currentPlayMode: LiveData<PlayMode> = _currentPlayMode

    // 当前音乐播放出现error
    private val _musicPlaybackError = MutableLiveData<Boolean>()
    val musicPlaybackError: LiveData<Boolean> = _musicPlaybackError

    //音乐播放错误
    fun updateError(musicPlaybackError: Boolean) {
        _musicPlaybackError.postValue(musicPlaybackError)
    }

    // 更新当前播放模式
    fun updateCurrentPlayMode(playMode: PlayMode) {
        _currentPlayMode.postValue(playMode)
    }

    // 更新当前播放的音乐
    fun updateCurrentMusic(songInfo: SongInfo) {
        _currentPlayedMusic.postValue(songInfo)
    }

    // 更新播放状态
    fun updatePlayingState(isPlaying: Boolean) {
        _isPlaying.postValue(isPlaying)
    }

    // 更新播放进度
    fun updateProgress(progress: Long) {
        _currentProgress.postValue(progress)
    }

    // 更新歌曲时长
    fun updateDuration(duration: Long) {
        _duration.postValue(duration)
    }

    // 重置所有状态
    fun reset() {
        _currentPlayedMusic.value = null
        _isPlaying.value = false
        _currentProgress.value = 0L
        _duration.value = 0L
    }
}